package cpr;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import cpr.context.Context;
import cpr.helper.ScoreHelper;

public class ContextPropertyReader {
	Context ctx = null;
	public ContextPropertyReader(Context _ctx) {
		this.ctx=_ctx;
		
		propertyHandlers = new HashMap();
    	
        propertyHandlers.put(PROPERTY_SPLIT_LIMIT,new IPropertyHandler() {
            public Object getValue() {
                return ctx.getSplitLimit();
            }
            public void setValue(Object value) {
                ctx.setSplitLimit(Integer.parseInt(value.toString()) );
            }
        });
        
        propertyHandlers.put(PROPERTY_SPLIT_LOW_LIMIT,new IPropertyHandler() {
            public Object getValue() {
                return ctx.getSplitLowLimit();
            }
            public void setValue(Object value) {
                ctx.setSplitLowLimit(Integer.parseInt(value.toString()) );
            }
        });
        
        propertyHandlers.put(PROPERTY_COMMAND,new IPropertyHandler() {
            public Object getValue() {
                return ctx.getStartupCommands();
            }
            public void setValue(Object value) {
            	StringTokenizer st = new StringTokenizer((String)value, ";");
            	int tokens = st.countTokens();
            	String[] cmds = new String[tokens];
            	int i = 0;
            	while (st.hasMoreTokens()) {
					String token = st.nextToken();
					cmds[i++] = token;
					
				}
                ctx.setStartupCommands(cmds);
            }
        });
        
    	propertyHandlers.put(PROPERTY_DATA,new IPropertyHandler() {
			public Object getValue() {
				return ctx.getRawDataFilename();
			}
			public void setValue(Object value) {
				ctx.setRawDataFilename((String)value);
				ctx.changeFilepath();
			}
    	});
    	propertyHandlers.put(PROPERTY_DATA_FOLDER,new IPropertyHandler() {
			public Object getValue() {
				return ctx.getDefaultDataFolder();
			}
			public void setValue(Object value) {
				ctx.setDefaultDataFolder((String)value);
				ctx.changeFilepath();
			}
    	});
    	propertyHandlers.put(PROPERTY_BUFFER_SIZE,new IPropertyHandler() {
			public Object getValue() {
				return ""+ctx.getBufferSize();
			}
			public void setValue(Object value) {
				ctx.setBufferSize(Integer.parseInt((String)value));
			}
    	});

    	propertyHandlers.put(PROPERTY_IMAGE_QUBE_CSV,new IPropertyHandler() {
			public Object getValue() {
				return ctx.getSubqubeDataFilename();
			}
			public void setValue(Object value) {
            	ctx.setSubqubeDataFilename((String)value);
			}
    	});

    	propertyHandlers.put(PROPERTY_VERBOSE,new IPropertyHandler() {
			public Object getValue() {
				return convertBooleanToString(ctx.isVerbose());
			}
			public void setValue(Object value) {
				ctx.setVerbose(convertStringToBoolean((String)value));
			}
    	});
    	propertyHandlers.put(PROPERTY_DIGITAL,new IPropertyHandler() {
			public Object getValue() {
				return convertBooleanToString(ctx.isDigital());
			}
			public void setValue(Object value) {
				ctx.setDigital(convertStringToBoolean((String)value));
			}
    	});
    	propertyHandlers.put(PROPERTY_RULES_TO_PROCESS,new IPropertyHandler() {
			public Object getValue() {
				return ctx.getRulesToLoad();
			}
			public void setValue(Object value) {
				ctx.setRulesToLoad(Integer.parseInt((String)value));
			}
    	});
    	propertyHandlers.put(PROPERTY_IMAGE_RULE,new IPropertyHandler() {
			public Object getValue() {
				return ctx.getMasterRule();
			}
			public void setValue(Object value) {
				ctx.setMasterRule(Integer.parseInt((String)value));
			}
    	});
    	
    	propertyHandlers.put(PROPERTY_IMAGE_SCORE,new IPropertyHandler() {
			public Object getValue() {
				return ctx.getRootScore();
			}
			public void setValue(Object value) {
				ScoreHelper helper = getContext().getScoreHelper();
        		int score = helper.convertStringToScore((String)value);
                ctx.setRootScore(score);
			}
    	});
	
	}
	
	public Context getContext() {
		return ctx;
	}
	
	public static String PROPERTY_DATA_FOLDER = "DATAFOLDER";
    
	public static String PROPERTY_IMAGE_RULE = "IMAGE_RULE";
    public static String PROPERTY_IMAGE_QUBE_CSV = "IMAGE_QUBE_CSV";
    public static String PROPERTY_IMAGE_SCORE = "IMAGE_SCORE";

    public static String PROPERTY_MODE = "MODE";
    public static String PROPERTY_MODE_VALUE_HOW_MANY_RULES_WAS_USED = "HOW-MANY-RULES-WAS-USED";
    public static String PROPERTY_MODE_VALUE_RULE_INFO = "RULE-INFO";
    public static String PROPERTY_MODE_VALUE_PRINT_CPR = "PRINT-CPR";
    public static String PROPERTY_MODE_VALUE_COPY = "COPY";
    public static String PROPERTY_MODE_VALUE_DUMP_RULES = "DUMP-RULES";
    public static String PROPERTY_MODE_VALUE_HL_RENUMBER = "HL-RENUMBER";
    public static String PROPERTY_MODE_VALUE_HL_IMAGE_QUBE = "HL-IMAGE-CUBE";
    public static String PROPERTY_MODE_VALUE_HL_PRINT_RECORDS_ACCORDING_TO_SCORE = "HL-PRINT-RECORDS-ACCORDING-TO-SCORE";
    public static String PROPERTY_MODE_VALUE_HL_ANALYSE = "HL-ANALYSE";
    public static String PROPERTY_MODE_VALUE_SCORE_SIZE = "SCORE-SIZE";
    public static String PROPERTY_MODE_VALUE_CLUSTER_SEARCH = "CLUSTER-SEARCH";
    public static String PROPERTY_MODE_BATCH = "BATCH";
    
    
    public static String PROPERTY_MODE_VALUE_TEST_PERF_INSTANCE = "TEST-PERF-INSTANCE";
    public static String PROPERTY_MODE_VALUE_TEST_PERF_RULE_COMP = "TEST-PERF-RULE-COMP";
       
    public static String PROPERTY_VERBOSE = "VERBOSE";
    public static String PROPERTY_DIGITAL = "DIGITAL";
    public static String PROPERTY_CPR_CSV = "CPR_CSV";
    public static String PROPERTY_DATA = "DATA";
    public static String PROPERTY_OUTPUT_FILE = "OUTPUT_FILE";
    public static String PROPERTY_HL_IMAGE = "HL_IMAGE";
    public static String PROPERTY_BUFFER_SIZE = "BUFFER_SIZE";
    public static String PROPERTY_RULES_CSV = "RULES_CSV";
    public static String PROPERTY_RULES_TO_PROCESS = "RULES_TO_PROCESS";

    public static String PROPERTY_SPLIT_LOW_LIMIT = "SPLIT_LOW_LIMIT";
    public static String PROPERTY_SPLIT_LIMIT = "SPLIT_LIMIT";
    
    public static String PROPERTY_COMMAND = "COMMAND";
    
    
    
    
    private static Set trueSet = null;
	private static Set falseSet = null;
	private Map propertyHandlers = null;
    static {
    	
    	trueSet = new HashSet();
    	trueSet.add("TRUE");
    	trueSet.add("YES");
    	trueSet.add("1");
    	
    	falseSet = new HashSet();
    	falseSet.add("FALSE");
    	falseSet.add("NO");
    	falseSet.add("0");
    	
    }
    
    public boolean convertStringToBoolean(String value) {
    	if ((value!=null)&&trueSet.contains(value.toUpperCase())) {
    		return true;
    	} else if ((value!=null)&&falseSet.contains(value.toUpperCase())) {
    		return false;
    	} else {
    		throw new RuntimeException("can not convert to boolean: "+value);
    	}
    }
    
    public String convertBooleanToString(boolean value) {
    	return Boolean.valueOf(value).toString();
    }
    
    public void setProperty(String name, Object value) {
    	IPropertyHandler handler = getPropertyHandler(name);
    	
    	if (handler!=null) {
    		handler.setValue(value);
    	} else {
    		throw new RuntimeException("Unknown property "+name);
    	}
    }
    
    public Object getProperty(String name) {
    	IPropertyHandler handler = getPropertyHandler(name);
    	if (handler!=null) {
    		return handler.getValue();
    	} else {
    		throw new RuntimeException("Unknown property "+name);
    	}
    }
    
    public IPropertyHandler getPropertyHandler(String name) {
    	name = name.toUpperCase();
    	IPropertyHandler handler = (IPropertyHandler)propertyHandlers.get(name);
    	return handler;
    }
    public Set getPropertyKeys() {
    	return propertyHandlers.keySet();
    }
    
 

}

